import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Case, provinceData, kindData, provinceEvent } from '@/interface/interface'

// export const useTurnStore = defineStore('turn', () => {
//   const allData = ref<Case[]>([])
//   const provinceList = ref<string[]>([])
//   const allProvinceData = ref<provinceData[]>([])
//   const provinceEventData = ref<kindData[]>([])
//   const provinceEvent = ref<provinceEvent>({ province: "", event: "" })
//   const allKindDate = ref([])
//   const causeOfAction = ref("")
//   const dialogVisible = ref<Case>()
//   const showOfDialog = ref(false)
//   const keyOfCauseOfAction = computed(() => {
//     const cases = allProvinceData.value.find((item) => item.name === provinceEvent.value.province)
//     const eventList = cases?.data.map((item) => item.caseType)
//     const eventSet = new Set(eventList)
//     const eventArray = Array.from(eventSet)
//     return eventArray
//   })

//   return {
//     allData,
//     allProvinceData,
//     provinceList,
//     provinceEventData,
//     provinceEvent,
//     allKindDate,
//     causeOfAction,
//     dialogVisible,
//     showOfDialog,
//     keyOfCauseOfAction,
//   }
// })

export const useTurnStore = defineStore('turn', {
  state:()=>({
    allData:[] as Case[],
    provinceList:[] as string[],
    allProvinceData:[] as provinceData[],
    provinceEventData:[] as kindData[],
    provinceEvent:{ province: "", event: "" } as provinceEvent,
    allKindDate:[] as any[],
    causeOfAction:"",
    dialogVisible:{} as Case,
    showOfDialog:false,
    clearProvinceList: false,
    chosenCaseIds: new Set<number>(),
    searchText: "",
    getData: false,
    turnData: "" as string,
    over: false,
  }),
  getters:{
    keyOfCauseOfAction(state){
        const cases = state.allProvinceData.find((item) => item.name === state.provinceEvent.province);
        const eventList = cases?.data.map((item) => item.caseType);
        const eventSet = new Set(eventList);
        const eventArray = Array.from(eventSet);
        return eventArray;
    },
  },
  actions:{
    appendChosenCaseIds(id:number){
      this.chosenCaseIds.add(id);
    },
    clearChosenCaseIds(){
      this.chosenCaseIds.clear();
    },
    clearProvinceSelection() {
      this.provinceList = [];
      this.clearProvinceList = true;
      setTimeout(() => {
        this.clearProvinceList = false;
      }, 0);
    }
  }
});
